var qt = {

	baseUrl : null,	
	queryUrl : 'lib/qt/ajax.query.inc.php',
	configName : null,
	isProfiling : 0,
	
	init : function()
	{
		var returnVal = qt.checkVariables();
		
		if (returnVal === true)
		{
			qt.regChangeConfig();
			qt.regEnableProfiling();
			qt.regTestButton();
			qt.regLoaderImage();			
		}
		else
		{
			return false;
		}
	},
	
	//check for internal js variables so everything would be correct
	checkVariables : function()
	{		
		if ( (!qt.baseUrl) || (qt.baseUrl == null) )
		{
			alert('Please set the BASE_URL constant in config.php');
			return false;
		}

		if ( (!qt.queryUrl) || (qt.queryUrl == null) )
		{
			alert('Please set queryUrl variable in assets/js/qt.js');
			return false;
		}
		
		return true;
				
	},
	
	
	// save the name of the dropdown box
	regChangeConfig : function()
	{		
		$('#lstDb').change(function(){
			qt.configName = $(this).val();
		}).trigger('change');
		
	},
	
	// enable or disable query profiling
	regEnableProfiling : function()
	{
		$('#chkProfiling').change(function(){		
			qt.isProfiling = 0;
			if (this.checked) 
			{ 
				qt.isProfiling = 1; 
			}
		});
	},
	
	// register event for the test query button
	regTestButton : function()
	{
		$('#btnTest').click(function(){
		
			//get the code from the editor
			var sqlCode = escape(editor.getCode());

			//send an ajax request with the appropiate config name
			jQuery.ajax({

				type: 'POST',
				url: qt.baseUrl + qt.queryUrl,
				data: { 'c': qt.configName, 'profiling': qt.isProfiling, 'sql': sqlCode },
				dataType: 'html',

				success: function(resp)
				{
					$('#result').html(resp);
				}
			});
			
		});
	},
	
	// just register the oading image
	regLoaderImage : function()
	{
		$('img.loader')
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
			});
	}	
}